<script setup>
import { ref, onMounted } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './conponents/ChannelSelect.vue'
import { artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format.js'

//文章列表
const articleList = ref([])
//总条数
const total = ref(0)
// loading
const loading = ref(false)

//获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(pageParams.value)
  console.log('artGetListService', res.data.data)
  articleList.value = res.data.data.records
  total.value = res.data.data.total
  loading.value = false
}
onMounted(() => {
  getArticleList()
})

//搜索逻辑
const onSearch = () => {
  pageParams.value.page = 1
  console.log('pageParams', pageParams.value)
  getArticleList()
}

//重置逻辑
const onReset = () => {
  pageParams.value = {
    page: 1,
    pageSize: 5,
    cateId: null,
    state: null
  }
  getArticleList()
}

//分页逻辑
const handleSizeChange = (size) => {
  pageParams.value.pageSize = size
  pageParams.value.page = 1
  getArticleList()
}

const handleCurrentChange = (page) => {
  pageParams.value.page = page
  getArticleList()
}

/* 编辑逻辑 */
const onEditArticle = (row) => {
  console.log(row)
}

/* 删除逻辑 */
const onDelArticle = (row) => {
  console.log(row)
}

/* 查询分页参数 */
const pageParams = ref({
  page: 1,
  pageSize: 5,
  cateId: null,
  state: null
})
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button>添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline class="demo-form-inline">
      <el-form-item label="文章分类:">
        <!--  Vue2=> v-model:value和@input的简写
        Vue3 => v-model:modelValue和@update:modelValue的简写 -->
        <channel-select v-model="pageParams.cateId"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select v-model="pageParams.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="articleTitle">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">
            {{ row.articleTitle }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="articleSortTitle"></el-table-column>
      <el-table-column label="发表时间" prop="createTime">
        <template #default="{ row }">
          {{ formatTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            type="primary"
            round
            @click="onEditArticle(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            type="danger"
            round
            @click="onDelArticle(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  分页区域 -->
    <el-pagination
      v-model:current-page="pageParams.page"
      v-model:page-size="pageParams.pageSize"
      :page-sizes="[2, 5, 7, 10]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
</template>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
